#include "./app_task.h"
#include "../driver/cmd_uart.h"
#include "../driver/log_uart.h"
#include "../driver/motor.h"
#include "./handler_task.h"
#include "../driver/music_uart.h"
#include "./status_task.h"
#include "../driver/key.h"
#include "./work_task.h"
#include "../driver/display.h"
#include "../service/device_info.h"

namespace task {

utils::Object<AppTask_t> appTask;

using namespace driver;

static uint8_t recv_buf[3][64];

void AppTask_t::loop() {
    using namespace utils;

    utils::ByteView_t bw;
    uint8_t index = 0;

    for (;;) {
        bw.buf = recv_buf[index];
        Error_t err = logUart->recv_proto(bw);
        if (err == ec::Ok) {
            logUart->write_ack();
            handlerTask->post(bw);
            index ++;
            if (index >= 3) {
                index = 0;
            }
        }
        
    }
}

void AppTask_t::run() {
    service::deviceInfo.load();

    cmdUart.Init();
    logUart.Init();
    motor.Init();
    key.Init();
    musicUart.Init();
    display.Init();
    
    osDelay(1000);

    display->show_price(Right);

    workTask->start();
    statusTask->start();
    handlerTask->start();

    logUart->printf("app run");
    logUart->printf("价格:%d", display->price());
    
    loop();
}

}

